package top.wangjiu.product;

import io.netty.util.internal.SocketUtils;
import top.wangjiu.product.service.OneServiceImpl;
import top.wangjiu.product.service.TwoServiceImpl;
import top.wangjiu.reactive.rpc.config.ServerConfiguration;
import top.wangjiu.reactive.rpc.registration.LocalServerRegister;
import top.wangjiu.reactive.rpc.registration.ServerRegister;
import top.wangjiu.reactive.rpc.server.RpcServer;
import top.wangjiu.service.interfaces.OneService;
import top.wangjiu.service.interfaces.TwoService;

/**
 * @ClassName ProductApp
 * @Description 原生方式使用Rpc
 * @Author wangjiu
 * @Date 2022/3/18 3:05 下午
 */

public class ProductApp {

    public static void main(String[] args) throws InterruptedException {

        //本地的服务注册器
        ServerRegister localServerRegister = new LocalServerRegister();
        localServerRegister.registService(OneService.class,0,new OneServiceImpl());
        localServerRegister.registService(TwoService.class,0,new TwoServiceImpl());

        ServerConfiguration configuration = new ServerConfiguration();
        configuration.setServerRegister(localServerRegister);
        //设置对外端口地址
        configuration.setServerAddress(SocketUtils.socketAddress("localhost",9000));
        //Server实例
        RpcServer rpcServer = new RpcServer(configuration);
        //开始提供服务
        rpcServer.start();
        System.out.println("服务提供者启动");
        Thread awaitThread = new Thread("server") {
            @Override
            public void run() {
                try {
                    rpcServer.waitStop();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        awaitThread.setContextClassLoader(ProductApp.class.getClassLoader());
        awaitThread.setDaemon(false);
        awaitThread.start();
    }

}
